<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="模板名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入模板名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="模板编码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入模板编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="模板类型" prop="type">
        <dict-select :options="dict.type.contract_type" v-model="queryParams.type" placeholder="请选择模板类型"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['setting:template:add']"
        >新增
        </el-button>
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['setting:template:edit']"
        >修改
        </el-button>
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['setting:template:remove']"
        >删除
        </el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="templateList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="模板名称" align="center" prop="name" width="180"/>
      <el-table-column label="模板编码" align="center" prop="code" width="180"/>
      <el-table-column label="模板类型" align="center" prop="type" width="180">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.contract_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="启用" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="'1'"
            :inactive-value="'0'"
            @change="enableChange(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['setting:template:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['setting:template:remove']"
          >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="preview(scope.row)"
          >查看模版
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改template对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label="模板名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入模板名称"/>
        </el-form-item>
        <el-form-item label="模板编码" prop="code">
          <el-input v-model="form.code" placeholder="请输入模板编码"/>
        </el-form-item>
        <el-form-item label="模板类型" prop="type">
          <dict-select :options="dict.type.contract_type" v-model="form.type" placeholder="请选择模板类型"/>
        </el-form-item>
        <el-form-item label="模板" prop="template">
           <span slot="label">
              模板
                <el-tooltip placement="top">
                  <div slot="content">
                    店铺合同动态信息填充方式：<br/>
                    例子: <br/>
                    模板原文： 用户 #{[name]} 在平台开通 名称为 #{[shopName]} 的店铺,.....<br/>
                    填充后：   用户 小明 在平台开通 名称为 小明精品店 的店铺,.....<br/>
                    <br/>
                    可使用信息：<br/>
                    #{[name]} --用户名称<br/>
                    #{[shopName]} --店铺名称<br/>
                    #{[phone]} --电话号码<br/>
                    #{[type]} --店铺类型<br/>
                    #{[email]} --邮箱<br/>
                    #{[createTime]} --开店时间<br/>
                    <br/>
                    投资合同模板信息填充方式：<br/>
                    模板原文： 用户 #{[userName]} 在平台购买产品名称为 #{[shopName]} 的项目,.....<br/>
                    填充后：   用户 小明 在平台购买产品名称为 #{[shopName]} 的项目,.....<br/>
                    可使用信息：<br/>
                    #{[name]} --用户名称<br/>
                    #{[contractCode]} --合同编码<br/>
                    #{[financialName]} --产品名称<br/>
                    #{[phone]} --电话号码<br/>
                    #{[interestRate]} --利率<br/>
                    #{[period]} --收益周期（天数）<br/>
                    #{[netValue]} --净值<br/>
                    #{[email]} --邮箱<br/>
                    #{[totalPrice]} --金额<br/>
                  </div>
                <i class="el-icon-question"></i>
                </el-tooltip>
              </span>
          <editor v-model="form.template" placeholder="请输入模版内容"
                  :height="500"/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="合同模版" :visible.sync="previewShow" width="560px" append-to-body>
      <div v-html="contract" style="max-height: 700px;overflow-y: scroll"></div>
    </el-dialog>
  </div>
</template>

<script>
import {addTemplate, delTemplate, listTemplate, offTemplate, updateTemplate} from "@/api/setting/template";
import DictSelect from "@/components/DictSelect/index.vue";

export default {
  name: "Template",
  components: {DictSelect},
  dicts: ['contract_type'],
  data() {
    return {
      contract: null,
      previewShow: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // template表格数据
      templateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        template: null,
        code: null,
        status: null,
        type: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          {required: true, message: "请输入模板名称", trigger: "blur"}
        ],
        template: [
          {required: true, message: "请输入模版内容", trigger: "blur"}
        ],
        code: [
          {required: true, message: "请输入模板编码", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询template列表 */
    getList() {
      this.loading = true;
      listTemplate(this.queryParams).then(response => {
        this.templateList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        template: null,
        code: null,
        status: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加模版";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = {...row}
      this.open = true;
      this.title = "修改模版";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            this.form.template = this.form.template.replace(/&lt;/g, "<").replace(/&gt;/g, ">");
            updateTemplate(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTemplate(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除？').then(function () {
        return delTemplate(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    //预览
    preview(data) {
      this.contract = data.template
      this.previewShow = true
    },
    //启用禁用
    enableChange(info) {
      info = {id: info.id, status: info.status}
      offTemplate(info).then(() => {
        this.$modal.msgSuccess("修改成功");
        this.getList();
      })
    },
  }
};
</script>
